<?php

namespace app\api\controller;


use app\admin\model\ShopFinanceDay;
use app\admin\model\ShopStore;
use app\admin\model\UserBase;
use app\admin\model\UserOrder;
use app\admin\model\UserSign;

class User extends Api
{

    public function index()
    {
        return $this->fetch();
    }

    public function income()
    {
        return $this->fetch();
    }

    public function incomeDetail()
    {
        return $this->fetch('income_detail');
    }

    /**
     * @title 获取收入明细
     * @author FashionJune
     */
    public function getFinanceFlow()
    {
        $date = input('date/s');
        $store_id = input('store_id/d') ?? api_error('缺少店铺参数');
        $store_info = ShopStore::find($store_id);
        ($store_info->bind_user != $this->user['user_id']) &&api_error('权限不足');
        $cashcard_use = input('type');
        $month = strtotime($date);
        $next_month = strtotime('+1month', $month);
        $map_flow = [
            ['store_id', '=', $store_id],
            ['date', 'between', "$month,$next_month"]
        ];
        $field = [
            "sum(price_total) as price",
            "sum(cashcard_total) as cashcard"
        ];
        $month_total = ShopFinanceDay::where($map_flow)->field($field)->select();
        $map_order = [
            ['store_id', '=', $store_id],
            ['pay_time', 'between', "$month,$next_month"]
        ];
        if ($cashcard_use == 1) {
            $map_order[] = ['cashcard_id', '>', 0];
        } elseif ($cashcard_use == 2) {
            $map_order[] = ['cashcard_id', '=', 0];
        }
        $flows = UserOrder::with('card')->where($map_order)->page($this->page, $this->per)->select();
        $flows_count = UserOrder::where($map_order)->count();

        api_success([
            'flows' => $flows,
            'flows_count' => $flows_count,
            'month_total' => $month_total[0]
        ]);
    }

    /**
     * @title 获取收入详情
     * @author FashionJune
     */
    public function getFinanceFlowDetail()
    {
        $id = input('id/d') ?? api_error('缺少订单参数');
        $order_info = UserOrder::with('user')->find($id);
        (!$order_info) && api_error('订单不存在');
        $order_info->status_name = strip_tags($order_info->status_name);
        ($order_info->uid != $this->user['user_id']) && api_error('权限不足') || api_success($order_info);
    }

    /**
     * @title 获取当前登录用户信息
     * @author FashionJune
     */
    public function getUserinfo()
    {
        if ($this->client == 'wechat') {
            $user_info = session('wechat_user');
        } elseif ($this->client == 'alipay') {
            $user_info = session('alipay_user');
        } else {
            return '非法请求';
        }
        $user_info['service_phone'] = sysconfig('rule', 'rule_service_phone');
        api_success($user_info);
    }


    /**
     * @title 签到页面
     * @author FashionJune
     */
    public function sign()
    {
        return $this->fetch();
    }

    /**
     * @title 签到提交
     * @author FashionJune
     */
    public function userSign()
    {
        $sign_time = input('sign_time');
        ($sign_time > time()) && api_error('建议您使用时间机器');
        $map_sign = [
            ['user_id', '=', $this->user['user_id']],
        ];
        if ($sign_time) {//补签
            $that_day_time = strtotime(date('Y-m-d', $sign_time));
            $insert_data['sign_time'] = $sign_time;
        } else {//正常签到
            $that_day_time = strtotime('today');
            $insert_data['sign_time'] = time();
        }
        $map_sign[] = ['sign_time', 'between', [$that_day_time, $that_day_time+86399]];
        $record = UserSign::where($map_sign)->find();
        if ($record) {
            api_error('请勿重复签到');
        }
        $insert_data['user_id'] = $this->user['user_id'];
        UserSign::create($insert_data);
        api_success('签到成功');
    }

    /**
     * @title 签到记录
     * @author FashionJune
     */
    public function signRecord()
    {
        //获取本周签到情况
        $today = strtotime('today');
        $day = date('w', $today);
        $begin = strtotime('-'. ($day+6)%7 .'days', $today);
        $end = strtotime('+'. (7-$day)%7 .'days', $today)+86399;
        //当前仅获取本周
        $map_sign = [
            ['user_id', '=', $this->user['user_id']],
            ['sign_time', 'between', [$begin, $end]]
        ];
        $list = UserSign::where($map_sign)->select();
        foreach ($list as &$item) {
            $item['week'] = date('w', $item['sign_time']);
            $item['week'] = ($item['week'] + 6) % 7;
        }
        api_list_return($list);
    }

}
